/// <reference path="jquery-1.3.2-vsdoc.js"/>
/*
COPYRIGHT (C) 2008 EPISERVER AB

THIS FILE IS PART OF SCRUM DASHBOARD.

SCRUM DASHBOARD IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF 
THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

SCRUM DASHBOARD IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT
EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER
GENERAL PUBLIC LICENSE FOR MORE DETAILS.

YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH SCRUM DASHBOARD. 
IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/

function openWorkItemID(id)
    {
        openWorkItemWithRefresh(id,0);
    }
    
    function openWorkItemWithRefresh(id, revisionid)
    {
        var wnd = window.open(tfsWebAccessRelative + 'wi.aspx?id=' + id,
			    'WorkItem' + id, 
			    'width=800,height=600,toolbar=no,scrollbars=yes,resizable=yes,status=yes');
			    
        if(revisionid>0)
        {
            var context = {};
            var callback = Function.createCallback(UnLoadingWorkItem, context);
            context.id = id;
            context.revisionid = revisionid;
            $addHandler(wnd, 'unload', callback);
        }
    }
      
    function UnLoadingWorkItem(evt,context)
    {
        Sys.Debug.trace("SD: Work item window was unloaded, checking for changes");
        var proxy = new ScrumDashboard.AjaxService();
        proxy.IsLatestVersion(context.id, context.revisionid, onIsLatestVersionSuccess, null, null);
    }
    
    function onIsLatestVersionSuccess(isLatestVersion)
    {
        if (!isLatestVersion) {
            $('#viewSprintButton').click();
            Sys.Debug.trace("SD: Opened work item has changed, reloading");
        }
        else {
            Sys.Debug.trace("SD: Opened work item has not changed");
        }
    }

    
   function showCreateProductBacklog()
   {
        $find(CreateProductBacklogPopupBehaviorClientID).show();
        $('#' + puTitleClientID).select();
   }
   
   
   function showTask(id,type)
   {
        $('#CreateTaskHeader').text(type);
        $('#CreateTaskType').val(type);
        
        $('#' + estimatedEffortRowClientID).css('display',(type!='Impediment' ? '' : 'none'));
        showDialog(createTaskPopupBehaviorClientID,id);
        $('#' + taskTitleClientID).select();
   }

   function showDialog(behaviourId, id)
   {
       $('#SelectedProductBacklogID').val(id);
       var modalPopupBehavior = $find(behaviourId);
       modalPopupBehavior.show();
   }

   function ExpandGoal()
   {      
       $('#goalText').text($('#goalHidden').val());
   }
        
    function hideDialog(behaviourId) 
    {
        var modalPopupBehavior = $find(behaviourId);
        modalPopupBehavior.hide();
    }
    
    function showAssignToDialog(id,requiresRefresh)
    {
        var modalPopupBehavior = $find(assignToClientID);
        modalPopupBehavior.show();
        
        if(requiresRefresh)
        {
            $('#' + assignToClientBTNID).click();
        }
    }
    
    function hideAssignToDialog()
    {
        var modalPopupBehavior = $find(assignToClientID);
        modalPopupBehavior.hide();
    }

    function TfsKeepAlive()
    {
        window.frames['tfsKeepAliveFrame'].location.href = tfsWebAccessRelative;
        Sys.Debug.trace("SD: TfsKeepAlive - " + Date());
    }
